using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class PIckCardPanel : MonoBehaviour
{
    private VisualElement rootElement;
    public VisualTreeAsset cardTemplate;
    private VisualElement cardContainer;
    private CardDataDetails currentCardData;
    public CardManager cardManager;

    private Button confirmButton;
    [Header("广播事件")]
    public ObjectEventSO finishPickCardEvent;

    private List<Button> cardButtonList = new();
    private void OnEnable()
    {
        rootElement = GetComponent<UIDocument>().rootVisualElement;
        cardContainer = rootElement.Q<VisualElement>("Container");
        confirmButton = rootElement.Q<Button>("ConfirmButton");

        for (int i = 0; i < 3; i++)
        {
            var card = cardTemplate.Instantiate();
            var data = cardManager.GetNewCardData();
            //初始化
            InitCard(card,data);
            var cardButton = card.Q<Button>("Card");
            cardButtonList.Add(cardButton);
            cardContainer.Add(card);
            
            cardButton.clicked += () => OnCardClickd(cardButton,data);
            confirmButton.clicked += OnConfirmClicked;
        }
    }

    private void OnConfirmClicked()
    {
        cardManager.UnlockCard(currentCardData);
        finishPickCardEvent.RaisEvent(null,this);
    }

    private void OnCardClickd(Button cardButton,CardDataDetails data)
    {
        currentCardData = data;
        for (int i = 0; i < cardButtonList.Count; i++)
        {
            if(cardButtonList[i] == cardButton)
                cardButtonList[i].SetEnabled(false);
            else
                cardButtonList[i].SetEnabled(true);
        }
        
    }

    public void InitCard(VisualElement card, CardDataDetails cardData)
    {
        var cardSpriteElement = card.Q<VisualElement>("CardSprite");
        var CardNameLabel = card.Q<Label>("CardNameLabel");
        var CardTypeLabel = card.Q<Label>("CardTypeLabel");
        var CardDescriptionLabel = card.Q<Label>("CardDescriptionLabel");
        var EnergyCostLabel = card.Q<Label>("EnergyCostLabel");
        var LevelSprite = card.Q<VisualElement>("LevelSprite");

        cardSpriteElement.style.backgroundImage = new StyleBackground(cardData.cardImage);
        
        if (cardData.isPrimaryCard == true)
        {
            CardNameLabel.text = "<color=\"black\">" + "初级·" + cardData.cardName + "</color>";
        }
        else if (cardData.isIntermediateCard == true)
        {
            CardNameLabel.text = "<color=\"blue\">" + "中级·" + cardData.cardName + "</color>";
        }
        else if (cardData.isSeniorCard == true)
        {
            CardNameLabel.text = "<color=\"purple\">" + "高级·" + cardData.cardName + "</color>";
        }
        
        CardTypeLabel.text = cardData.cardType switch
        {
            CardType.Attack => "攻击",
            CardType.Defense => "技能",
            CardType.Abilities => "能力",
            _ => throw new ArgumentOutOfRangeException()
        };
        CardDescriptionLabel.text = cardData.cardDescription;
        EnergyCostLabel.text = cardData.cardCost.ToString();
        
        // LevelSprite.RemoveFromClassList("Primary");
        // LevelSprite.RemoveFromClassList("Intermdiate");
        // LevelSprite.RemoveFromClassList("Senior");
        // switch (cardData.cardLevel)
        // {
        //     case CardLevel.Primary:
        //         LevelSprite.AddToClassList("Primary");
        //         break;
        //     case CardLevel.Intermediate:
        //         LevelSprite.AddToClassList("Intermdiate");
        //         break;
        //     case CardLevel.Senior:
        //         LevelSprite.AddToClassList("Senior");
        //         break;
        // }
    }
}
